<template>
  <el-dialog
    center
    v-dialogDrag
    v-if="isShow"
    :title="title"
    :width="dialogWidth"
    :before-close="close"
    :visible.sync="isShow"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="right"
      class="mt20 overflow pl15 pr15"
    >
      <el-row :gutter="20" v-if="!editDialog">
        <el-col :span="24" class="mb15">
          <el-form-item
            label="部门名称："
            prop="name"
            :label-width="labelWidth"
            class="required"
          >
            <el-input
              v-model="form.name"
              autocomplete="off"
              placeholder="请输入部门名称"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="editDialog">
        <el-col :span="24" class="mb15">
          <el-form-item
            label="部门名称："
            prop="name"
            :label-width="labelWidth"
          >
            <span class="theme">{{ deptOldName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="editDialog">
        <el-col :span="24" class="mb15">
          <el-form-item
            label="部门更名："
            prop="name"
            :label-width="labelWidth"
            class="required"
          >
            <el-input
              v-model="form.name"
              autocomplete="off"
              placeholder="请输入部门名称"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="!editDialog">
        <el-col :span="24" class="mb15">
          <el-form-item
            label="联系电话："
            prop="contactNumber"
            :label-width="labelWidth"
            class="required"
          >
            <el-input
              v-model="form.contactNumber"
              autocomplete="off"
              placeholder="请输入联系电话"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="!editDialog">
        <el-col :span="24" class="mb15">
          <el-form-item
            label="部门描述："
            prop="remark"
            :label-width="labelWidth"
            class="required"
          >
            <el-input
              type="textarea"
              autocomplete="off"
              v-model="form.remark"
              placeholder="请输入部门描述"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer align-center tiling inline-block"
      v-loading="loading"
    >
      <el-button @click="close">取 消</el-button>
      <el-button class="theme-btn" @click="handleConfirm('form')"
        >提 交</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import "@/utils/drag.js";
import { editDept, addDept } from "@/apis/user";
import { vPhoneOrLandline, vName, vlength } from "@/utils/verification.js";

export default {
  name: "Dept_dialog",
  data() {
    return {
      form: {
        name: "",
        remark: "",
        contactNumber: ""
      },
      deptList: [],
      centerList: [],
      buildTypeList: [],
      labelWidth: "95px",
      dialogWidth: "440px",
      rules: {
        name: {
          validator: vName(2, 10, "请输入部门名称"),
          trigger: "blur"
        },
        contactNumber: {
          validator: vPhoneOrLandline,
          trigger: "blur"
        },
        remark: {
          validator: vlength(2, 400, "请输入描述"),
          trigger: "blur"
        }
      }
    };
  },
  computed: {
    isShow() {
      return this.addDialog || this.editDialog;
    }
  },
  watch: {
    addDialog() {
      this.resetForm();
    },
    editDialog() {
      this.form = {
        id: this.deptForm.id,
        name: this.deptForm.label
      };
    }
  },
  methods: {
    handleConfirm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$store.dispatch("load/setLoading", true);
          this.editDialog ? this.editDept() : this.addDept();
        }
      });
    },
    editDept() {
      const params = {
        id: this.form.id,
        name: this.form.name,
       };

      // editDept(params).then(res => {
      //   if (res && res.code === "200") {
      //     this.$emit("update");
      //     this.$emit("close");
      //     this.$notify({
      //       title: res.msg,
      //       type: "success"
      //     });
      //   }
      // });
      this.$emit("update");
      this.$emit("close");
      this.$notify({
        title: "操作成功!",
        type: "success"
      });
    },
    addDept() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const params = {
            name: this.form.name,
            remark: this.form.remark,
            contactNumber: this.form.contactNumber,
           };

          // addDept(params).then(res => {
          //   if (res && res.code === "200") {
          //     this.$emit("close");
          //     this.$emit("update");
          //     this.$notify({
          //       title: res.msg,
          //       type: "success"
          //     });
          //   }
          // });
          this.$emit("update");
          this.$emit("close");
          this.$notify({
            title: "操作成功!",
            type: "success"
          });
        }
      });
    },
    close() {
      this.resetForm();
      this.$emit("close");
    },
    resetForm() {
      this.form = {
        name: "",
        remark: "",
        contactNumber: ""
      };
    }
  },
  props: [
    "title",
    "loading",
    "deptForm",
    "addDialog",
    "editDialog",
    "deptOldName"
  ]
};
</script>

<style scoped></style>
